package com.view
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class MiniMap extends Sprite
	{
		private var mini:Sprite;
		private var miniRole:Sprite;
		private var boder:Sprite;
		private var map:Bitmap;
		private var _data:BitmapData;
		private var mapWidth:int = 150;
		private var stageWidth:int = 800;
		private var stageHeigth:int = 500;
		private var proportion:Number ;
		public function MiniMap()
		{
			mini = new Sprite();
			boder = new Sprite();
			map = new Bitmap();
			miniRole = new Sprite();
			addChild(map);
			addChild(boder);
			addChild(mini);
			//mini.addChild(miniRole);
			mini.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		public function set data(obj:Object):void
		{
			_data = obj as BitmapData;
			map.bitmapData = _data;
			map.width = this.mapWidth;
			map.height = map.width /( _data.width / _data.height);
			
			proportion = (this.width / _data.width );
			boder.graphics.clear();
			boder.graphics.lineStyle(1,0xffffff);
			boder.graphics.beginFill(0xffffff,0.05);
			boder.graphics.drawRect(0,0,map.width,map.height);
			boder.graphics.endFill();
			
			mini.graphics.clear();
			mini.graphics.beginFill(0xffffff,0.3);
			mini.graphics.drawRect(0,0,stageWidth * proportion , stageHeigth * proportion);
			mini.graphics.endFill();
			/*
			miniRole.graphics.clear();
			miniRole.graphics.beginFill(0xff0000);
			miniRole.graphics.drawCircle(0,0,3);
			miniRole.graphics.endFill();
			miniRole.x = (mini.width - miniRole.width) / 2;
			miniRole.y = (mini.height - miniRole.height) / 2;
			*/
			mini.buttonMode = true
			mini.x = (map.width - mini.width) / 2;
			mini.y = (map.height - mini.height) / 2;
			moveBigMap(-mini.x /( mini.width / 800), -mini.y / (mini.height / 500));
			
		}
		private function onClick(e:MouseEvent):void
		{
			if(e.currentTarget == miniRole || e.target == miniRole)
				return ;
				
			var movex:Number,movey:Number;
			if(mouseX > mini.width / 2 && map.width - mouseX > mini.width / 2)
			{
				movex = mouseX - mini.width / 2;
			}
			else if(mouseX < mini.width / 2)
			{
				movex = 0;
			}
			else
			{
				movex = map.width - mini.width;
			}
			if(mouseY > mini.height / 2 && map.height - mouseY > mini.height /2)
			{
				movey = mouseY - mini.height / 2;
			}
			else if(mouseY < mini.height / 2)
			{
				movey = 0;
			}
			else 
			{
				movey = map.height - mini.height;
			}
			moveMiniMap(movex,movey);
			moveBigMapByTween(-movex /( mini.width / 800), -movey / (mini.height / 500));
			
		}
		private function onDown(e:MouseEvent):void
		{
			this.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
			mini.addEventListener(MouseEvent.MOUSE_UP,onUp);
		}
		private function onMove(e:MouseEvent = null):void
		{
			var movex:Number,movey:Number;
			
			if(mouseX > mini.width / 2 && map.width - mouseX > mini.width / 2)
			{
				movex = mouseX - mini.width / 2;
			}
			else if(mouseX < mini.width / 2)
			{
				movex = 0;
			}
			else
			{
				movex = map.width - mini.width;
			}
			if(mouseY > mini.height / 2 && map.height - mouseY > mini.height /2)
			{
				movey = mouseY - mini.height / 2;
			}
			else if(mouseY < mini.height / 2)
			{
				movey = 0;
			}
			else 
			{
				movey = map.height - mini.height;
			}
			
			mini.x = movex;
			mini.y = movey;
			moveBigMap(-mini.x /( mini.width / 800), -mini.y / (mini.height / 500));
			
		}
		private function onUp(e:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
			mini.removeEventListener(MouseEvent.MOUSE_UP,onUp);
		}
		public function moveMiniMap(movex:Number,movey:Number):void
		{
			var length:Number = Math.sqrt((mini.x - movex) * (mini.x - movex) + (mini.y - movey) * (mini.y - movey));
			var times:Number = length / (proportion * MyMap_2.ins.speed);
			TweenLite.to(mini,times,{x:movex,y:movey});
		}
		private function moveBigMap(mx:Number,my:Number):void
		{
			if(this && this.parent && this.parent.parent)
			{
				(this.parent.parent as MyMap_2).moveMap(mx,my);
			}
		}
		private function moveBigMapByTween(mx:Number,my:Number):void
		{
			if(this && this.parent && this.parent.parent)
			{
				(this.parent.parent as MyMap_2).moveTweenMap(mx,my);
			}
		}
	}
}